<template>
	<view class="classlist">
		<view class="loadinglayout" v-if="!classList.length && !noData">
			<uni-load-more status="loading"></uni-load-more>
		</view>
		<view class="content">
			<navigator class="item" :url="`/pages/preview/preview?id=`+item._id" v-for="item in classList" :key="item._id">
				<image :src="item.smallPicurl" mode="aspectFill"></image>
			</navigator>
		</view>
		<view class="loadinglayout" v-if="classList.length || noData">
			<uni-load-more :status="noData ? 'noMore' : 'loading'"></uni-load-more>
		</view>
		<view class="safe-area-inset-bottom">
			
		</view>
	</view>
</template>

<script setup>
import { ref } from "vue";
import { getClassListApi, userWallListApi } from "@/api/apis.js"
import { onLoad, onReachBottom, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { gotoHome } from '@/utils/common.js'

const classList = ref([])
const noData = ref(false)
const queryParams = {
	pageNum: 1,
	pageSize: 12
}
let pageName;

onLoad((e) => {
	const { id = null, name = null, type = null } = e
	// 正常分类列表
	if(id) queryParams.classid = id
	// 我的下载或我的评分记录列表
	if(type) queryParams.type = type
	
	pageName = name
	uni.setNavigationBarTitle({
		title: name
	})
	
	getClassList()
})

onReachBottom(() => {
	if(noData.value) return
	queryParams.pageNum++
	getClassList()
})

const getClassList = async () => {
	let res;
	if(queryParams.classid) {
		res = await getClassListApi(queryParams)
	}
	if(queryParams.type) {
		res = await userWallListApi(queryParams)
	}
	classList.value = [...classList.value, ...res.data]
	uni.setStorageSync('classList', classList.value)
	if(queryParams.pageSize > res.data.length) noData.value = true
}

// 分享给好友
onShareAppMessage(() => {
	return {
		title: '咸虾米壁纸-' + pageName,
		path: '/pages/classlist/classlist?id=' + queryParams.classid + "&name=" + pageName,
	}
})

// 分享给朋友圈
onShareTimeline(() => {
	return {
		title: '咸虾米壁纸-' + pageName,
		query: 'id=' + queryParams.classid + "&name=" + pageName
	}
})
</script>

<style lang="scss" scoped>
.classlist {
	.content {
		padding: 5rpx;
		display: grid;
		gap: 5rpx;
		grid-template-columns: repeat(3, 1fr);
		.item {
			height: 440rpx;
			image {
				width: 100%;
				height: 100%;
				display: block;
			}
		}
	}
}
</style>
